package com.ruoyi.project.storage.metadataproperty.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.storage.metadata.service.IMetadataService;
import com.ruoyi.project.storage.metadataproperty.domain.MetadataProperty;
import com.ruoyi.project.storage.metadataproperty.service.IMetadataPropertyService;

/**
 * 数据字段Controller
 * 
 * @author shixiangwen
 * @date 2020-02-27
 */
@Controller
@RequestMapping("/storage/metadataproperty")
public class MetadataPropertyController extends BaseController {
	private String prefix = "storage/metadataproperty";

	@Autowired
	private IMetadataPropertyService metadataPropertyService;

	@Autowired
	private IMetadataService metadataService;

	@RequiresPermissions("storage:metadataproperty:view")
	@GetMapping()
	public String metadataproperty() {
		return prefix + "/metadataproperty";
	}

	@RequiresPermissions("storage:metadataproperty:view")
	@GetMapping("/{metadataId}")
	public String metadataproperty(@PathVariable("metadataId") String metadataId, ModelMap mmap) {
		mmap.put("metadata", metadataService.selectMetadataById(metadataId));
		return prefix + "/metadataproperty";
	}

	/**
	 * 查询数据字段列表
	 */
	@RequiresPermissions("storage:metadataproperty:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(MetadataProperty metadataProperty) {
		startPage();
		List<MetadataProperty> list = metadataPropertyService.selectMetadataPropertyList(metadataProperty);
		return getDataTable(list);
	}

	/**
	 * 导出数据字段列表
	 */
	@RequiresPermissions("storage:metadataproperty:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(MetadataProperty metadataProperty) {
		List<MetadataProperty> list = metadataPropertyService.selectMetadataPropertyList(metadataProperty);
		ExcelUtil<MetadataProperty> util = new ExcelUtil<MetadataProperty>(MetadataProperty.class);
		return util.exportExcel(list, "metadataproperty");
	}

	/**
	 * 新增数据字段
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存数据字段
	 */
	@RequiresPermissions("storage:metadataproperty:add")
	@Log(title = "数据字段", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(MetadataProperty metadataProperty) {
		return toAjax(metadataPropertyService.insertMetadataProperty(metadataProperty));
	}

	/**
	 * 修改数据字段
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		MetadataProperty metadataProperty = metadataPropertyService.selectMetadataPropertyById(id);
		mmap.put("metadataProperty", metadataProperty);
		return prefix + "/edit";
	}

	/**
	 * 修改保存数据字段
	 */
	@RequiresPermissions("storage:metadataproperty:edit")
	@Log(title = "数据字段", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MetadataProperty metadataProperty) {
		return toAjax(metadataPropertyService.updateMetadataProperty(metadataProperty));
	}

	/**
	 * 删除数据字段
	 */
	@RequiresPermissions("storage:metadataproperty:remove")
	@Log(title = "数据字段", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(metadataPropertyService.deleteMetadataPropertyByIds(ids));
	}
}
